<template>
  <div class="login-form-wrap">
    <div class="login-form-title">登录</div>
    <a-form :model="loginForm" class="login-form" @finish="onFinish">
      <a-form-item
        label="姓名"
        name="username"
        :rules="[{ required: true, message: '请输入姓名' }]"
        class="login-form-item"
      >
        <a-input v-model:value="loginForm.username">
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        label="密码"
        name="password"
        :rules="[{ required: true, message: '请输入密码' }]"
        class="login-form-item"
      >
        <a-input-password v-model:value="loginForm.password">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>
      <a-form-item>
        <div class="site-form-button-wrap">
          <a-button type="primary" html-type="submit" class="login-form-button"> 登录 </a-button>
          <a-button type="primary" @click="handleRegister"> 注册 </a-button>
        </div>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup>
import { reactive, computed, ref } from 'vue'
import router from '@/router'
import { useUserStore } from '@/stores'
import { message } from 'ant-design-vue'

const emit = defineEmits(['register'])

const userStore = useUserStore()
const loginForm = reactive({
  username: '',
  password: ''
})

const disabled = computed(() => {
  return !(loginForm.username && loginForm.password)
})

const onFinish = (values) => {
  userStore
    .LoginByUserName(values.username, values.password)
    .then(() => {
      message.success('登录成功')
      router.push({ path: '/' })
    })
    .catch((e) => {
      message.error(e.message)
    })
}

const handleRegister = () => {
  emit('register')
}
</script>

<style scoped lang="scss">
.login-form-wrap {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  height: 300px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  text-align: center;
  padding: 20px 10px;
  transform-origin: center center;
  transition: all 1s ease-in-out;
  opacity: 1;
  z-index: 1;
  .login-form-title {
    font-size: 30px;
    margin-bottom: 30px;
  }

  .login-form-item {
    margin-bottom: 30px;
  }

  .site-form-button-wrap {
    width: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 0 5px;
  }
}

@keyframes rotate {
  from {
    transform: rotateY(0deg) translate(-50%, -50%);
    opacity: 1;
  }
  to {
    transform: rotateY(180deg) translate(-50%, -50%);
    opacity: 0;
  }
}
</style>
